package com.gomyb.admin.core.shiro;

import com.gomyb.admin.core.shiro.util.ShiroUtil;
import com.gomyb.result.ResultCode;
import com.gomyb.utils.WriteUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 权限验证
 */
@Log4j2
public class MyAuthFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse rep, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) rep;
        if (request.getMethod().toUpperCase().equals("OPTIONS")) {
            chain.doFilter(req, rep);
            return;
        }
        Subject subject = SecurityUtils.getSubject();
        Object username = subject.getPrincipal();
        if (null != username && "admin".equals(username.toString())) {
            chain.doFilter(req, rep);
            return;
        }
        //url校验
        if (subject.isAuthenticated()) {
            Set<String> permissions = ShiroUtil.getPermissions();
            if (null != permissions && permissions.size() > 0) {
                String requestURI = request.getRequestURI();
                String[] uriSplit = requestURI.split("/");
                for (String permission : permissions) {
                    if (permission.equals(requestURI)) {
                        chain.doFilter(req, rep);
                        return;
                    }
                    //检查是否为restful风格
                    if (permission.contains("{") && permission.contains("}")) {
                        String[] perSplit = permission.split("/");
                        if (uriSplit.length == perSplit.length) {
                            int count = 0;
                            for (int i = 0; i < uriSplit.length; i++) {
                                if (perSplit[i].contains("{") && perSplit[i].contains("}")) {
                                    count++;
                                    continue;
                                }
                                if (uriSplit[i].equals(perSplit[i])) {
                                    count++;
                                } else {
                                    break;
                                }
                            }
                            if (count == uriSplit.length) {
                                chain.doFilter(req, rep);
                                return;
                            }
                        }
                    }
                }
            }
        }
        WriteUtils.writeNO(request, response, ResultCode.NO_PERMISSION);
    }

    @Override
    public void destroy() {

    }
}
